<template>
  <div class="home-container">
    <!-- 欢迎信息卡片 -->
    <el-card class="welcome-card">
      <template #header>
        <div class="welcome-header">
          <el-avatar :size="64" :src="'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'" />
          <div class="welcome-info">
            <h2>欢迎回来，用户"{{ userInfo.nickname || userInfo.username }}"</h2>
            <p>今天是美好的一天，让我们开始工作吧！</p>
          </div>
        </div>
      </template>
    </el-card>

    <!-- 数据统计卡片 -->
    <el-row :gutter="20" class="data-overview">
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="data-header">
              <el-icon class="icon"><Document /></el-icon>
              <span>文章总数</span>
            </div>
          </template>
          <div class="data-number">{{ statistics.articleCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="data-header">
              <el-icon class="icon"><Collection /></el-icon>
              <span>分类总数</span>
            </div>
          </template>
          <div class="data-number">{{ statistics.categoryCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="data-header">
              <el-icon class="icon"><View /></el-icon>
              <span>浏览总数</span>
            </div>
          </template>
          <div class="data-number">{{ statistics.viewCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="data-header">
              <el-icon class="icon"><Star /></el-icon>
              <span>点赞总数</span>
            </div>
          </template>
          <div class="data-number">{{ statistics.likeCount }}</div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Document, Collection, View, Star } from '@element-plus/icons-vue'
import request from '../utils/request'

const userInfo = reactive({
  username: '',
  nickname: '',
  email: '',
  avatar: ''
})

const statistics = ref({
  articleCount: 0,
  categoryCount: 0,
  viewCount: 0,
  likeCount: 0
})

// 获取用户信息
const getUserInfo = async () => {
  try {
    const res = await request.get('/user/info')
    if (res.code === 200) {
      Object.assign(userInfo, res.data)
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

// 获取统计数据
const getStatistics = async () => {
  try {
    const res = await request.get('/statistics/count')
    console.log('完整的统计数据:', res)
    statistics.value = {
      articleCount: res.articleCount || 0,
      categoryCount: res.categoryCount || 0,
      viewCount: 0,
      likeCount: 0
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
    statistics.value = {
      articleCount: 0,
      categoryCount: 0,
      viewCount: 0,
      likeCount: 0
    }
  }
}

onMounted(() => {
  getUserInfo()
  getStatistics()
})
</script>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 120px);
}

.welcome-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.welcome-header {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px;
}

.welcome-info h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
  font-weight: 600;
}

.welcome-info p {
  margin: 8px 0 0;
  color: #909399;
  font-size: 14px;
}

.data-overview {
  margin-top: 20px;
}

.data-card {
  border-radius: 8px;
  transition: all 0.3s;
}

.data-card:hover {
  transform: translateY(-5px);
}

.data-header {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #606266;
}

.data-header .icon {
  font-size: 20px;
}

.data-number {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  text-align: center;
  padding: 20px 0;
}

:deep(.el-card__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-card) {
  border: none;
  overflow: visible;
}
</style> 